<template>
	<view class="content">
		<view class="bg">
			<text>计算机云考勤</text>
		</view>
		
		<view class="usernameinput" v-if="showbtu==0">
			<input class="inputstyle" placeholder="请输入账号" focus v-model="username" />
		</view>
		<view class="usernameinput" v-if="showbtu==0">
			<input class="inputstyle" placeholder="请输入密码(默认工号/学号)" password="true" focus v-model="password" />
		</view>
		<view style="margin-top: 20rpx;" v-if="showbtu==0">
			<u-checkbox-group >
				<u-checkbox v-model="checkboxValue" shape="circle" labelSize="30rpx" labelColor="white" @change="handleCheckboxChange"
					label="我是学生 无法登录?  勾选我试试"></u-checkbox>
			</u-checkbox-group>
		</view>
		
		<view style="margin-top: 40rpx;" v-if="showbtu==0">
			<u-button text="账号登录" color="linear-gradient(to right, rgb(0, 85, 255), rgb(85, 170, 255))" shape="circle"
				size="large" customStyle="width: 650rpx" @click="login"></u-button>
		</view>
		
		<view style="margin-top: 100rpx;" v-if="showbtu==1">
			<u-button text="微信授权登录" color="linear-gradient(to right, rgb(0, 85, 255), rgb(85, 170, 255))" shape="circle"
				size="large" customStyle="width: 650rpx" @click="wxlogin"></u-button>
		</view>
		
		<view style="margin-top: 20rpx;">
			<text style="color: white;">换个登录方式?</text>
			<view @click="showbtu=1" v-if="showbtu==0" style="margin-left: 190rpx;margin-top: -45rpx;">
				<text style="color: #aaffff;margin-left: 30rpx;
					text-decoration-line: underline;" >微信登录</text>
			</view>
			
			<view @click="showbtu=0" v-if="showbtu==1" style="margin-left: 190rpx;margin-top: -45rpx;">
				<text style="color: #aaffff;margin-left: 20rpx;
					text-decoration-line: underline;" >账号登录</text>
			</view>
		</view>
		<view v-if="showbtu==1" style="margin-top: 256rpx;"></view>
		<view class="aboutus">
			<text>@湖南工学院计算机科学与工程学院计算机云考勤</text><br>
			<text>技术支持：计算机科学与工程学院 | 衡阳市源辰信息科技有限公司</text><br>
			<text>地址：湖南省衡阳市珠晖区衡花路18号</text>
		</view>
	</view>
</template>

<script>
	import {
		AVATARURL,
		HNIT_URL,
		MONITOR,
		STUNAME,
		USERNAME,
		IDENTITY,
		TNAME,
		SESSION_KEY,
		OPENID,
		WXNAME
	} from "@/constants/index.js";
	export default {
		data() {
			return {
				//账号
				username: "",
				//密码
				password: "",
				//显示按钮状态
				showbtu: 1,
				//学生的复选框
				checkboxValue: true,
				//微信名
				nickName: "",
				//头像
				avatarUrl: "",
				//微信授权对象
				userInfo: {},
				code: "",
				session_key: "",
				openId: "",
				//是否为班长
				spare: 0
			}
		},
		methods: {
			//我是学生的状态
			handleCheckboxChange() {
				if (this.checkboxValue == true) {
					this.checkboxValue = false
				} else {
					this.checkboxValue = true
				}
			},
			//登录
			login() {
				//为空不能输入
				if (this.username == "" || this.password == "") {
					uni.showToast({title: "请输入正确的登录信息",icon: "none",duration: 2500})
					return;
				}
				uni.showLoading({title: "正在登录..."})
				let params = {
					username: this.username,
					password: this.password,
					//复选框的状态
					checkValue: this.checkboxValue
				}
				this.$request.post(HNIT_URL + "login", params).then(res => {
					uni.hideLoading();
					uni.showToast({title: res.msg,icon: "none",duration: 2000,mask: true})
					//登录成功之后
					if (res.code == 1) {
						//学号或者工号存入缓存
						uni.setStorageSync(USERNAME, this.username)
						//判断返回的identity
						if (res.data.identity == 1) {
							//领导身份存入缓存
							uni.setStorageSync(IDENTITY, 1)
							uni.reLaunch({url: "../manage/home/index"})
						} else if (res.data.identity == 0) {
							//学生
							uni.setStorageSync(IDENTITY, 0);
							uni.redirectTo({url: "../student/home/index"})
						} else if (res.data.identity == 3) {
							//老师
							uni.setStorageSync(IDENTITY, 3);
							uni.reLaunch({url: "../teacher/home/index"})		
						} else if(res.data.identity==4){
							//即使老师也是领导
							uni.setStorageSync(IDENTITY, 4);
							uni.reLaunch({url: "../manage/home/index"})	
						} else {
							uni.showToast({title: "小程序暂未对您开放权限,请您谅解!",icon: "none",duration: 2000});
						}
					}
				}, err => {
					return uni.showToast({title: err.msg,icon: 'none',duration: 3000,mask: true});
				});
			},
			//微信授权
			wxlogin() {			
				uni.showLoading({title:"正在登录中...",mask:true})
				let username = uni.getStorageSync(USERNAME)
				uni.login({
					provider: "weixin",
					success: res => {
						this.code = res.code
					}
				})
				uni.getUserProfile({
					desc: '获取你的个人信息',
					success: (res) => {
						//点击授权之后关闭弹出层
						let params = {code: this.code,username: username}
						this.$request.post(HNIT_URL + "wx/wxloginagain", params).then(rs => {
							if (rs.code == 1) {
								this.userInfo = res.userInfo
								this.session_key = rs.data.session_key
								this.openId = rs.data.openId
								uni.setStorageSync(SESSION_KEY, this.session_key)
								uni.setStorageSync(OPENID, this.openId)
								uni.setStorageSync(WXNAME, this.userInfo.nickName);
								uni.setStorageSync(AVATARURL, this.userInfo.avatarUrl)
								uni.setStorageSync(USERNAME, rs.data.stuid)
								if (rs.data.identity == "student") {
									uni.hideLoading();
									uni.reLaunch({
										url: "../student/home/index"
									})
									uni.setStorageSync(IDENTITY, 0)
								} else if (rs.data.identity == "teacher") {
									uni.hideLoading();
									uni.reLaunch({
										url: "../teacher/home/index"
									})
									uni.setStorageSync(IDENTITY, 3)
								} else if (rs.data.identity == "leader") {
									uni.hideLoading();
									uni.reLaunch({
										url: "../manage/home/index"
									})
									uni.setStorageSync(IDENTITY, 1)
								} else if (rs.data.identity == "leaderAndTeacher") {
									uni.hideLoading();
									uni.reLaunch({
										url: "../manage/home/index"
									})
									uni.setStorageSync(IDENTITY, 4)
								}else if (rs.data.identity == "leaderAndTea") {
									uni.hideLoading();
									uni.reLaunch({
										url: "../manage/home/index"
									})
									uni.setStorageSync(IDENTITY, 4)
								}  else {
									uni.hideLoading();
									uni.showToast({title: "小程序暂未对您开放权限,请您谅解!",icon: "none",duration: 3000})
								}
							}else if (rs.code==-1){
								uni.hideLoading();
								uni.showToast({title: "已在别处登录",duration: 3000,icon: "none",mask: true});
							}else  {
								//如果授权失败是因为未进行与学号绑定，授权成功返回个人信息
								if(rs.msg == "当前微信用户未绑定工号或学号!"){
									this.showbtu = 0;
								}
								uni.showToast({title: rs.msg,duration: 3000,icon: "none",mask: true});
							}
						}, err => {
							return uni.showToast({title: "网络走丢了...",icon: "none",duration: 3000,mask: true});
						})
					},
					fail: res => {
						uni.showToast({title: "拒绝授权",icon: "none",duration: 3000});
					}
				})
			},
		}
	}
</script>

<style>
	.aboutus{
		color: aliceblue;
		margin-top: 400rpx;
		font-size: 25rpx;
		text-align: center;
	}
	.bg {
		font-size: 100rpx;
		margin-top: 400rpx;
		backdrop-filter: blur(10px); /* 设置高斯模糊效果*/
		background-color: rgba(255, 255, 255, 0.9);
		-webkit-background-clip: text;
		background-clip: text;
		color: transparent;
		font-family: FZSuXSLSJW;
	}

	.inputstyle {
		text-align: center;
		margin-top: 25rpx;
		margin-left: 30rpx;
		font-size: 40rpx;
		width: 580rpx;
	}

	.usernameinput {
		margin-top: 40rpx;
		width: 640rpx;
		border-radius: 40rpx;
		height: 90rpx;
		backdrop-filter: blur(5px); /* 设置高斯模糊效果 */
		background-color: rgba(255, 255, 255, 0.9);
	}

	.title {
		margin-top: 400rpx;
		font-family: FZSuXSLSJW;
		font-size: 50rpx;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	page {
		background: linear-gradient(to top, #55aaff 0%, #120f6d 100%);
		background-size: 100% 200%;
		animation: aurora 10s infinite;
		
	}
	@keyframes aurora {
	  0% {
	    background-position-y: 0;
	  }
	  50% {
	    background-position-y: 100%;
	  }
	  100% {
	    background-position-y: 0;
	  }
	}
</style>
